/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DataConverters;

import DataConverters.DataFormatters.IDataFormatter;
import DataConverters.DataFormatters.formattersimpl.CSVFormatter;
import DataConverters.DataFormatters.formattersimpl.Excel7Formatter;
import DataConverters.DataFormatters.formattersimpl.Excel97Formatter;
import DataConverters.DataFormatters.formattersimpl.GDAFormatter;
import DataConverters.DataFormatters.formattersimpl.JTableFormatter;
import DataConverters.DataReaders.IDataReader;
import DataConverters.interfaces.IConvertable;
import DataConverters.DataReaders.readersImpl.ExcelReader;
import DataConverters.DataReaders.readersImpl.GDAReader;
import DataConverters.DataReaders.readersImpl.JTableReader;
import DataConverters.DataReaders.readersImpl.XlsxReader;
import DataConverters.DataReaders.readersImpl.defaultDataReader.DefaultDataReader;
import DataConverters.exceptions.DataConvertException;
import DataConverters.exceptions.DataReaderException;

import common.gui.progress.IProgressItem;
import DataConverters.utils.WarningMessagesContainer;
import FileInformation.utils.DataType;
import java.util.HashMap;

/**
 *
 * @author Nobody
 */
public class DataConverter {

    private IDataReader reader;
    private IDataFormatter formatter;

    public void convert(DataType inputType, DataType outputType,
            IConvertable input,
            IProgressItem progressItem,
            IConvertable output,
            HashMap<String, Object> outputProperties) throws DataConvertException {

        reader = createReader(inputType, progressItem, input);
        formatter = createFormatter(outputType);

        try {
            reader.read();

            formatter.setSortedSet(reader.getReadedData());
            formatter.setProperties(outputProperties);
            formatter.format(output);
        } catch (DataReaderException ex) {
            throw new DataConvertException("Cannot convert data", ex);
        }

    }

    public boolean areWarningMessages() {
        return reader.hasWarningMessages();
    }

    public WarningMessagesContainer getWarningMessagesContainer() {
        return reader.getWarningMessagesContainer();
    }

    private IDataReader createReader(DataType inputType, IProgressItem progressItem, IConvertable input) {
        IDataReader newReader = null;
        switch (inputType) {
            case EXCEL:
                newReader = new ExcelReader(progressItem, input);
                break;
            case EXCEL_7:
                newReader = new XlsxReader(progressItem, input);
                break;
            case GDA:
                newReader = new GDAReader(progressItem, input);
                break;
            case JTABLE:
                newReader = new JTableReader(progressItem, input);
                break;
            default:
                newReader = new DefaultDataReader(progressItem, input);
                break;
        }
        return newReader;
    }

    private IDataFormatter createFormatter(DataType outputType) {
        IDataFormatter newFormatter = null;

        switch (outputType) {
            case EXCEL:
                newFormatter = new Excel97Formatter();
                break;
            case EXCEL_7:
                newFormatter = new Excel7Formatter();
                break;
            case GDA:
                newFormatter = new GDAFormatter();
                break;
            case JTABLE:
                newFormatter = new JTableFormatter();
                break;
            default:
                newFormatter = new CSVFormatter();
                break;
        }
        return newFormatter;
    }
}
